package com.ruoyi.web.controller.saas;

import com.bonuo.saas.domain.ResearchProject;
import com.bonuo.saas.service.ResearchProjectService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/saas/researchProject")
@Api(tags = "研发项目管理")
public class ResearchProjectController extends BaseController {
    @Autowired
    private ResearchProjectService researchProjectService;

    @ApiOperation(value = "查询研发项目信息",httpMethod = "GET")
    @GetMapping("/list")
    public TableDataInfo list(ResearchProject researchProject, Integer pageNum, Integer pageSize){
        if(pageNum != null && pageSize != null){
            startPage();
        }
        List<ResearchProject> list = researchProjectService.selectResearchProjectList(researchProject);
        return getDataTable(list);
    }

    @ApiOperation(value = "添加研发项目信息",httpMethod = "POST")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ResearchProject researchProject){
        return toAjax(researchProjectService.insertResearchProject(researchProject));
    }

    @ApiOperation(value = "修改研发项目信息",httpMethod = "PUT")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody ResearchProject researchProject){
        return toAjax(researchProjectService.updateResearchProject(researchProject));
    }

    @ApiOperation(value = "删除研发项目信息",httpMethod = "DELETE")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id){
        return toAjax(researchProjectService.deleteResearchProjectById(id));
    }

    @ApiOperation(value = "导出研发项目信息",httpMethod = "POST")
    @PostMapping("/export")
    public void exportData(ResearchProject researchProject, HttpServletResponse response){
        List<ResearchProject> list = researchProjectService.selectResearchProjectList(researchProject);
        ExcelUtil<ResearchProject> util = new ExcelUtil<ResearchProject>(ResearchProject.class);

        if(researchProject.getType() == 1){
            util.showColumn(
                    "name","source","level","applyDate","applicationUnit","checkDate","participant","personnelForCommunicating","status",
                    "subsidyAmount","arrivalAmount","remark");
            util.exportExcel(response, list, "项目申报数据");
        }else if(researchProject.getType() == 2){
            util.showColumn(
                    "projectType","name","level","applyDate","applicationUnit","participant","personnelForCommunicating","status",
                    "startDate","validDate","subsidyAmount","arrivalAmount","remark");
            util.exportExcel(response, list, "资质荣誉数据");
        }

    }

}
